#Include ..\utils\HwndUtils.ahk
#Include ..\service_api\TradeService.ahk
#Include ..\const\LightningSellConst.ahk
#Include ..\const\LightningBuyConst.ahk
#Include ..\const\ClientConstant.ahk
#Include ..\utils\TipUtils.ahk
#Include ..\utils\KeyboardUtils.ahk
#Include ..\const\KeyConst.ahk

; 卖出交易的实现类
class SellTradeServiceImpl extends TradeService {
    findLightningWindow(){
        ; 激活应用的窗口
        HwndUtils.activateWindow()
        ; 卖出窗口
        sell_window_text := LightningSellConst.LIGHTNING_SELL_WINDOW_TITLE
        ok:=FindText(&X:="wait", &Y:=3, 0,0,0,0,0,0,sell_window_text)  ; 等待3秒等图像出现\
        if(ok){
            TipUtils.tip("找到了闪电卖出的窗口")
            return
        }
        ; 如果没有找到闪电卖出的窗口,反而存在了闪电买入的窗口,则需要先关闭闪电买入的窗口
        buy_window_text := LightningBuyConst.LIGHTNING_BUY_WINDOW_TITLE
        ok:=FindText(&X:="wait", &Y:=3, 0,0,0,0,0,0,buy_window_text)
        if(ok){
            TipUtils.tip("找到了闪电买入的窗口,开始关闭闪电买入窗口")
            FindText().Click(X, Y, "L")
            KeyboardUtils.esc()
            TipUtils.tip("关闭完成")
        }
        ; 再次激活应用窗口
        HwndUtils.activateWindow()
        KeyboardUtils.selectAll()
        KeyboardUtils.input(KeyConst.LIGHTING_SELL_KEY)
        KeyboardUtils.enter()
        ; 23   闪电卖出(普通或担保) 交易 字符的查找
        Text := LightningSellConst.FIND_SELL_DESC
        if (ok:=FindText(&X, &Y, 872-150000, 502-150000, 872+150000, 502+150000, 0, 0, Text))
        {
           KeyboardUtils.enter()
        }
    }
    findPriceAmount(position){
        A_Clipboard := ""
        while (A_Clipboard != position)
        {
            Text := LightningSellConst.SELL_AMOUNT
                    ok:=FindText(&X:="wait", &Y:=ClientConstant.SCREEN_QUERY_TIME, 0,0,0,0,0,0,Text)
                    if(ok){
                        FindText().Click(X+ClientConstant.X_OFFSET, Y, "L")
                        KeyboardUtils.selectAll()
                        KeyboardUtils.input(position)
                        KeyboardUtils.selectAll()
                        KeyboardUtils.copy()
                         ClipWait
                    }

        }

    }
    findOperationButton(){
        Text:=LightningSellConst.SELL_BUTTON_TEXT
        ok:=FindText(&X:="wait", &Y:=ClientConstant.SCREEN_QUERY_TIME, 0,0,0,0,0,0,Text)
        if(ok){
            Sleep 1000
            FindText().Click(X, Y, "L")
        }
    }

    findSucceTip(){
        Text:=LightningSellConst.SELL_SUCCESS_TIP_TEXT
        ok:=FindText(&X:="wait", &Y:=ClientConstant.SCREEN_QUERY_TIME, 0,0,0,0,0,0,Text)
        if(ok){
            TipUtils.tip("卖出完成")
            KeyboardUtils.enter()
        }
    }
}